/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.dw.service.impl;

import com.icinfo.cloud.provider.punish.dw.dto.DwCaseCountQueryDto;
import com.icinfo.cloud.provider.punish.dw.mapper.DwCaseOrgDailyMapper;
import com.icinfo.cloud.provider.punish.dw.model.DwCaseOrgDaily;
import com.icinfo.cloud.provider.punish.dw.service.IDwCaseOrgDailyService;
import com.icinfo.cloud.provider.punish.dw.vo.DwCaseOrgDailyVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述: 机构案件每日统计更新 dw_case_org_daily 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年05月27日
 */
@Service
@Transactional(rollbackFor = {Error.class, Exception.class})
public class DwCaseOrgDailyServiceImpl extends MyBatisServiceSupport implements IDwCaseOrgDailyService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(DwCaseOrgDailyServiceImpl.class);

    @Resource
    private DwCaseOrgDailyMapper dwCaseOrgDailyMapper;


    /**
     * 每日同步
     */
    @Override
    public void statisticsCaseInfo() {
        // 分别查询
        List<DwCaseOrgDaily> ccl = dwCaseOrgDailyMapper.getCaseCountData();
        List<DwCaseOrgDaily> dcl = dwCaseOrgDailyMapper.getDocumentCountData();
        List<DwCaseOrgDaily> fcl = dwCaseOrgDailyMapper.getFileCountData();
        //数据拼装
        for (DwCaseOrgDaily dd : ccl) {
            dd.setId(StringUtils.uuid());
            for (DwCaseOrgDaily dc : dcl) {
                if (StringUtils.isNotBlank(dd.getOrgCode()) && StringUtils.isNotBlank(dd.getDeptCode())){
                    if (dd.getOrgCode().equals(dc.getOrgCode()) && dd.getDeptCode().equals(dc.getDeptCode())){
                        dd.setDocumentCount(dc.getDocumentCount());
                    }
                }
            }
            for (DwCaseOrgDaily fc : fcl){
                if (StringUtils.isNotBlank(dd.getOrgCode()) && StringUtils.isNotBlank(dd.getDeptCode())){
                    if (dd.getOrgCode().equals(fc.getOrgCode()) && dd.getDeptCode().equals(fc.getDeptCode())){
                        dd.setFileCount(fc.getFileCount());
                    }
                }
            }
        }
        if (!ccl.isEmpty()){
            dwCaseOrgDailyMapper.deleteAll();
            // 超过500 分页插入
            if (ccl.size() > 500){
                List<DwCaseOrgDaily> saveList = new ArrayList<>();
                int page = BigDecimal.valueOf(ccl.size()).divide(BigDecimal.valueOf(500), RoundingMode.UP).intValue();
                for (int i = 0; i < page; i++) {
                    saveList = ccl.stream().skip((i - 1) * 500L).limit(500L).collect(Collectors.toList());
                    dwCaseOrgDailyMapper.saveList(saveList);
                }
                return;
            }
            dwCaseOrgDailyMapper.saveList(ccl);
        }
    }

    @Override
    public List<DwCaseOrgDailyVo> getPageList(DwCaseCountQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
//        if (StringUtils.isBlank(dto.getDeptCode())){
//            dwCaseOrgDailyVos = dwCaseOrgDailyMapper.getListVoByOrgCode(dto.getOrgCode());
//        } else {
        List<DwCaseOrgDailyVo> dwCaseOrgDailyVos = dwCaseOrgDailyMapper.getListVo(dto);
//        }
        return dwCaseOrgDailyVos;
    }
}